home *** CD-ROM | disk | FTP | other *** search
- /*
- File: MyCaptureAppShell.c
-
- Contains: MyCaptureApp shell.
-
- Written by: John Wang
-
- Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <1> 04/04/94 JW Created.
-
- To Do:
- Wait till leave word rectangle
- little L processor that Kat doesn't think should really be neccessary
-
-
- */
-
-
- #include <Types.h>
- #include <Memory.h>
- #include <QuickDraw.h>
- #include <Palettes.h>
- #include <QDOffscreen.h>
- #include <Errors.h>
- #include <Fonts.h>
- #include <Dialogs.h>
- #include <Windows.h>
- #include <Menus.h>
- #include <Events.h>
- #include <Devices.h>
- #include <DiskInit.h>
- #include <OSUtils.h>
- #include <Resources.h>
- #include <ToolUtils.h>
- #include <AppleEvents.h>
- #include <EPPC.h>
- #include <Gestalt.h>
- #include <Processes.h>
- #include <Balloons.h>
- #include <Aliases.h>
- #include <MixedMode.h>
- #include <Scrap.h>
- #include <LowMem.h>
- #include <Timer.h>
- #include <Serial.h>
- #include <Speech.h>
- #include <TextUtils.h>
-
- #include <string.h>
-
- #include "PalmTypes.h"
- #include "ErrorBase.h"
- #include "ErrorMgr.h"
- #include "VFSMgr.h"
-
-
- #include "CommandStrings.h"
-
- #include "GenericSerial.h"
-
-
- #include "MyUtils.h"
- #include "SerialFSServ.h"
-
- #define kMouseRegionWidth 120
- #define kMouseRegionHeight 50
-
-
- #define rMonitor 1000
- #define kSendButton 1
- #define kStopButton 2
- #define kQuitButton 3
- #define kMsgBox 4
- #define kSendSpinner 5
- #define kRcvSpinner 7
- #define kHoldFlag 9
- #define kBreakButton 10
-
- #define rSerDataRsrc 128
- #define rSpinnerIcon 1000
- #define rHeldIcon 128
- #define rNotHeldIcon 129
-
- #define rPortOpenALRT 256
- #define kReset 2
-
- #define kCtlEnable 0
- #define kCtlDisable 255
- #define kSerBufSize 16384
- #define kSerRdSize 64
- #define kSerConfig baud57600 + noParity + data8 + stop10
- #define kBreakLength 666 // serial break length in milliseconds
-
- #define drvrName 0x12 // offset to driver name in 'DRVR' std. header
-
- #define dOpened 5
- #define dRAMBased 6
- #define drvrActive 7 // Device Manager DCtlFlag bits
-
- #define kSerStatus 8
- #define kSerClrBrk 11 // Serial Driver csCodes
- #define kSerSetBrk 12
- #define kSerHShakeDTR 14
- #define breakR0 128 // mask for break bit in SCC RR0 -- See TN #56
- #define breakErr 8 // mask for break bit in cumErrs -- System 7.0
-
- #define kInputDriver "\p.CIn"
- #define kOutputDriver "\p.COut"
- //#define kInputDriver "\p.IrIn"
- //#define kOutputDriver "\p.IrOut"
-
-
-
- #define kUpArrowKey 0x7E1E
- #define kLeftArrowKey 0x7B1C
- #define kDownArrowKey 0x7D1F
- #define kRightArrowKey 0x7C1D
-
- #define kSpaceBarKey 0x3120
- #define kPeriodKey 0x2F2E
- #define kEscapeKey 0x351B
- #define kEKey 0x0E45
- #define kMKey 0x2E4D
- #define k0Key 0x1D30
- #define k1Key 0x1231
- #define k2Key 0x1332
- #define k3Key 0x1433
- #define kPKey 0x2350
-
-
-
- Err PrvTranslateMacErr(OSErr macErr);
- Err PrvTranslatePalmPath(const Char *palmPathP);
- Err PrvFindDirID(const Char *dirPathNameP, long *dirID);
-
-
- #ifdef powerc
- //QDGlobals qd;
- #endif
-
- Boolean gDone; // Set to true if you want to Application to kindly quit.
-
- WindowPtr gWin = 0;
-
- #define kPathBufferLen 600
- #define kDirectoryFileRefToken 'dirt' // FileRef we use for an open directory. Can only have 1 open directory at a time.
-
- unsigned char gPathBuffer[kPathBufferLen];
- long gBaseDirID;
- long gIterateDirID;
- short gCurVRefNum;
- Boolean gDirOpen;
- CInfoPBRec gMyCPB;
-
- //GWorldPtr gRecognizeThis = 0;
- unsigned long gLastTicks = 0;
-
-
- Ptr gSerBuf; // serial driver receive buffer
- // gBitBucket, // input buffer
- // gOutBuf; // output buffer
-
- IOParam gSendPB; // for PBWrite calls to send data
- short gSysVersion,
- gOutRefNum, gInRefNum;
-
- long gBitBucketCount = 0;
-
-
-
- void Initialize (void);
- short SerInitialize (void);
- void CleanUp (void);
- Boolean OpenSERD (void);
- void CloseSERD (void);
- OSErr SendData (void);
- void CheckSerData (void);
- void ProcessBitBucket(void);
- void EventLoop(char top);
-
- /* ------------------------------------------------------------------------- */
-
- void main()
- {
-
- // Initialize here. Set the yield time too.
- // bestTimeBase
- // err = ProfilerInit(collectDetailed,microsecondsTimeBase,50,10);
- Initialize();
- // FindTargetPSN();
-
- if(SerInitialize() != noErr) DebugStr("\pCan't init serial stuff");
-
- // open the Serial Driver
- if (! OpenSERD()) DebugStr("\pCan't open serial drvr");
-
- // SendData();
-
-
- // Event loop.
- for ( ; ; ) {
- EventLoop(true);
- }
- }
-
- void EventLoop(char top)
- {
- EventRecord myEvent;
- long yieldTime = 0;
- WindowPtr foundWindow;
- short windowPart;
- Boolean isEvent;
- GrafPtr savePort;
- GDHandle saveGD;
- OSErr err=noErr;
-
- // Get the event.
- isEvent = WaitNextEvent(everyEvent, &myEvent, yieldTime, nil);
-
- if(myEvent.what == keyDown)
- {
- myEvent.what = keyDown;
- }
-
- // CheckSerData();
- // ProcessBitBucket();
-
- if(top) ServeSomething();
-
- // SendEvents();
-
- // If the event is unhandled by app specific event handling, then we proceed.
- if ( isEvent ) {
- switch ( myEvent.what ) {
-
- case mouseDown:
- // Get current port and device.
- GetPort(&savePort);
- saveGD = GetGDevice();
-
- // Set the port and gdevice to the window if we own the window.
- // We can then assume anytime the event occured in one of our windows,
- // that the port and gdevice are set correctly.
- windowPart = FindWindow(myEvent.where, &foundWindow);
- SetPort(foundWindow);
- SetGDevice(GetMainDevice());
-
- // Handle the different mouse down events.
- switch ( windowPart ) {
- case inSysWindow:
- SystemClick(&myEvent, foundWindow);
- break;
- case inMenuBar:
- DoCommand(MenuSelect(myEvent.where));
- break;
- case inContent:
- break;
- case inDrag:
- // If dragging one of the application's windows, then handle it.
- // However, if we are dragging a zoomed window, we
- // must remember to save the new window location into the
- // zoomed rect in the data handle. Otherwise, the event
- // manager will think that we are no longer zoomed.
- {
- WStateData *zoomData;
- Rect windowRect;
-
- // Get window location before drag.
- GetGlobalWindow(foundWindow, &windowRect);
-
- // Drag window.
- DragWindow (foundWindow, myEvent.where, &qd.screenBits.bounds);
- // MyDrag(foundWindow, myEvent.where);
-
- // If the windowRect in global coordinates matches the zoom rect,
- // then assume that we are dragging the zoomed window. update
- // zoom rect.
- zoomData = (WStateData *) *(((CWindowPeek) foundWindow)->dataHandle);
- if ( EqualRect(&(zoomData->stdState), &windowRect) ) {
- GetGlobalWindow(foundWindow, &windowRect);
- zoomData = (WStateData *) *(((CWindowPeek) foundWindow)->dataHandle);
- zoomData->stdState = windowRect;
- }
- }
- break;
- case inGrow:
- break;
- case inGoAway:
- // Handle clicking on the go away. If it is the clip window,
- // then hide it.
- if ( TrackGoAway (foundWindow, myEvent.where) ) {
- BringToFront(foundWindow);
- // MyClose();
- DisposeWindow(foundWindow);
- if(foundWindow == gWin) gWin = 0;
- }
- break;
- case inZoomIn:
- case inZoomOut:
- break;
- default:
- break;
- }
-
- // Restore port and device.
- SetPort(savePort);
- SetGDevice(saveGD);
-
- break;
- case keyDown:
- case autoKey:
- if ( myEvent.modifiers & cmdKey ) {
- if ( myEvent.what == keyDown ) {
- DoCommand(MenuKey(myEvent.message & charCodeMask));
- }
- }
- break;
- case updateEvt:
- // Handle update events for window and clip window.
- foundWindow = (WindowPtr) myEvent.message;
- GetPort(&savePort);
- saveGD = GetGDevice();
- SetPort(foundWindow);
- SetGDevice(GetMainDevice());
- BeginUpdate(foundWindow);
- EndUpdate(foundWindow);
- SetPort(savePort);
- SetGDevice(saveGD);
- break;
- case diskEvt:
- // This handles a bad disk. Otherwise the disk will not eject.
- if ( myEvent.message >> 16 ) {
- Point tempPoint;
- tempPoint.v = 50; tempPoint.h = 50;
- DIBadMount(tempPoint, myEvent.message);
- }
- break;
- case activateEvt:
- break;
- case app4Evt:
- switch ( myEvent.message >> 24 ) {
- case suspendResumeMessage:
- // yieldTime = MyYieldTime(myEvent.message & 0x01);
- break;
- default:
- DebugStr("\pUnexpected suspend/resume message.");
- }
- break;
- default:
- break;
- }
- }
-
- // If DoneFlag set, then quit.
- if ( gDone ) {
- // err = ProfilerDump((unsigned char *)"\pProfileInfo");
- // ProfilerTerm();
- CloseSERD(); // close the Serial Driver
- CleanUp();
-
- ExitToShell();
- }
-
- }
-
-
- /* ------------------------------------------------------------------------- */
-
- void Initialize()
- {
- // OSErr err;
- Handle myMenu;
- // Rect bounds;
-
- // Initialize Managaer.
- MaxApplZone();
- MoreMasters(); MoreMasters();
- MoreMasters(); MoreMasters();
- MoreMasters(); MoreMasters();
- MoreMasters(); MoreMasters();
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitDialogs(nil);
- InitCursor();
-
- // Set up menus.
- myMenu = GetNewMBar(kMENUBAR);
- SetMenuBar(myMenu);
- DisposeHandle(myMenu);
- AppendResMenu(GetMenuHandle(kMENU_APPLEID), 'DRVR');
- DrawMenuBar();
-
- // Setup other globals.
- gDone = false;
-
- /* // Create window:
- bounds.top = 50;
- bounds.left = 50;
- bounds.bottom = bounds.top + kMouseRegionHeight;
- bounds.right = bounds.left + kMouseRegionWidth;
-
- gWin = NewCWindow(0, &bounds, "\pBits", true, documentProc, (WindowPtr)-1, true, 0);
- SetPort(gWin);
-
- // Create gworld:
- bounds.top = bounds.left = 0;
- bounds.right = kMouseRegionWidth;
- bounds.bottom = kMouseRegionHeight;
-
- err = NewGWorld(&gRecognizeThis, 16, &bounds, nil, nil, 0);
- */
- gPathBuffer[0] = '\0';
- gDirOpen = false;
-
- return;
- }
-
- short SerInitialize (void)
- {
- long result;
- OSErr gestErr;
-
- gestErr = Gestalt(gestaltSystemVersion, &result);
- gSysVersion = result;
-
- // gBitBucket = NewPtr(kSerBufSize);
- // if (gBitBucket == NULL) return MemError();
-
- gSerBuf = NewPtr(kSerBufSize);
- if (gSerBuf == NULL) return MemError();
-
- // gOutBuf = NewPtr(kSerBufSize);
- // if (gOutBuf == NULL) return ResError();
-
- return noErr;
- }
-
-
- void CleanUp (void)
- {
-
- // DisposeHandle((Handle) ghSendPB);
- // DisposePtr(gBitBucket);
- // DisposePtr(gOutBuf);
- DisposePtr(gSerBuf);
- }
-
-
-
- void DoCommand(long mResult)
- {
- short theMenu, theItem;
- Str255 myStr;
- GrafPtr savePort;
- GDHandle saveGD;
-
- theItem = LoWord(mResult);
- theMenu = HiWord(mResult);
-
- if ( theItem != 0 || theMenu != 0 ) {
- switch ( theMenu ) {
- case kMENU_APPLEID:
- if ( theItem == 1 ) {
- Alert(kALERT_ABOUT, nil);
- } else {
- GetMenuItemText(GetMenuHandle(kMENU_APPLEID), theItem, myStr);
- GetPort(&savePort);
- saveGD = GetGDevice();
- (void) OpenDeskAcc(myStr);
- SetPort(savePort);
- SetGDevice(saveGD);
- }
- break;
-
- case kMENU_FILEID:
- switch ( theItem ) {
- case kMENU_FILENEW:
- // MyNew();
- break;
- case kMENU_FILECLOSE:
- // MyClose();
- break;
- case kMENU_FILEQUIT:
- gDone = true;
- break;
- default:
- ReportFatal("\pError in handling file menu:", theItem);
- }
- break;
-
- default:
- ReportFatal("\pError in handling menu:", theMenu);
- }
- }
- HiliteMenu(0);
- }
-
-
-
-
- Boolean OpenSERD (void)
- {
- OSErr openOutErr, openInErr;
- OSErr setBufErr, setCfgErr, setHskErr;
- SerShk hskFlags;
- // long finalTicks;
- Boolean takeOverPort = true;
- // Boolean openAOut, openAIn;
-
- /* openAOut = AssertDrvrOpen(kOutputDriver, &gOutRefNum) == noErr;
- openAIn = AssertDrvrOpen(kInputDriver, &gInRefNum) == noErr;
- if (openAOut || openAIn) {
- if (takeOverPort = CautionAlert(rPortOpenALRT, nil) == kReset) {
- if (openAIn) {
- KillIO(gInRefNum);
- CloseDriver(gInRefNum);
- }
- if (openAOut) {
- KillIO(gOutRefNum);
- CloseDriver(gOutRefNum);
- }
- }
- }
-
- if (takeOverPort) {
- */
- openOutErr = OpenDriver(kOutputDriver, &gOutRefNum);
-
- // if(openOutErr == portInUse)
- // {
- // KillIO(gOutRefNum);
- // CloseDriver(gOutRefNum);
- // openOutErr = OpenDriver(kOutputDriver, &gOutRefNum);
- // }
-
- openInErr = OpenDriver(kInputDriver, &gInRefNum);
- // if(openInErr == portInUse)
- // {
- // KillIO(gInRefNum);
- // CloseDriver(gInRefNum);
- // openInErr = OpenDriver(kInputDriver, &gInRefNum);
- // }
-
- if (openOutErr == noErr && openInErr == noErr) {
-
- // It's always good to first set a non-default input buffer, if desired.
- // There is no output buffering, so specify only the input driver.
-
- setBufErr = SerSetBuf(gInRefNum, gSerBuf, kSerBufSize);
-
- hskFlags.fXOn = false;
- hskFlags.fCTS = true;
- hskFlags.xOn = 0x11;
- hskFlags.xOff = 0x13;
- hskFlags.errs = 0;
-
- if (gSysVersion >= 0x0700) {
- hskFlags.evts = 0; // I can use new means of break detection.
- }
- else {
- hskFlags.evts = breakEvent; // I need the driver to post break events.
- }
-
- hskFlags.fInX = false;
- hskFlags.fDTR = true;
-
- // SerHShake() does not support full DTR/CTS hardware handshaking. You
- // accomplish the same thing and more with a Control call and csCode 14.
- // You only need to specify hskFlags once, to the output driver.
-
- setHskErr = Control(gOutRefNum, kSerHShakeDTR, (Ptr) &hskFlags);
-
- // Now reset both input and output drivers with the same configuration.
- // Only a single call to the output driver is necessary to do this.
- // Differing concurrent input/output baud rates are not supported.
-
- setCfgErr = SerReset(gOutRefNum, kSerConfig);
-
- }
- else DebugStr("\pCan't open driver");
- // }
-
- return takeOverPort;
- }
-
-
-
- void CloseSERD (void)
- {
- OSErr killErr, closeOutErr, closeInErr;
-
- killErr = KillIO(gInRefNum);
- closeInErr = CloseDriver(gInRefNum);
-
- killErr = KillIO(gOutRefNum);
- closeOutErr = CloseDriver(gOutRefNum);
-
- }
-
-
- /*
-
- void CheckSerData (void)
- {
- OSErr checkBufErr, serRdErr;
- long charCount;
-
- checkBufErr = SerGetBuf(gInRefNum, &charCount);
- if (checkBufErr == noErr) {
-
- // The general strategy here is this: if number of available characters
- // meets a certain minimum threshold, then I read in everything in the
- // buffer. If I get delayed, I'll catch up quickly.
-
- // if (charCount != 0 && charCount >= reqBytes) {
- if (charCount > 0) {
-
- serRdErr = FSRead(gInRefNum, &charCount, &gBitBucket[gBitBucketCount]);
- if (serRdErr == noErr) {
- gBitBucketCount += charCount;
- // gBitBucket[gBitBucketCount] = 0xFF; // append an 'unfinished string' terminator
- }
- }
- }
- }
- */
- /*
- OSErr SendData (void)
- {
- gSendPB.ioCompletion = 0;//(ProcPtr)0;// SendCompRout;
- gSendPB.ioRefNum = gOutRefNum;
- gSendPB.ioBuffer = "Twas Brillig & the Slithy Toves";//gpOutBuf;
- gSendPB.ioReqCount = 31;//kSerBufSize;
-
- // return PBWriteAsync((ParmBlkPtr) gSendPB); // asynchronous self-sustaining sends
- return PBWriteSync((ParmBlkPtr) &gSendPB); // asynchronous self-sustaining sends
-
- }
- */
- /*
- Searches the receive buffer, looking for
- completed (\r terminated) command strings to execute.
- */
- /*
- void ProcessBitBucket(void)
- {
- char* s = gBitBucket;
- short i = 0;
- Boolean foundString = false;
-
- while(foundString == false && i < gBitBucketCount)
- {
- if(s[i] == '\r')
- {
- foundString = true;
- s[i] = 0; // null terminate the string
- }
- if(s[i] == 0xFF)
- {
- foundString = false;
- break;
- }
-
- i++;
- }
-
- if(foundString == true)
- {
- if(strcmp(s, "Prev") == 0) {
- s = "Previous";
- }
- c2pstr(s);
- SpeakString((unsigned char*)s);
- p2cstr((unsigned char*)s);
-
- // ProcessString(s);
-
- gBitBucketCount -= i;
- BlockMove(&s[i], &s[0], gBitBucketCount);
- }
-
- return;
- }
-
- */
- /*
- Given a command string, set the right
- event up to be posted to the CD/DVD player
- */
- /*
- void ProcessString(char* s)
- {
-
- if(strcmp(s, kPlayCmd) == 0 ||
- strcmp(s, kPauseCmd) == 0)
- {
- // PostEvent(keyDown, ' ');
- gMessageToPost = kSpaceBarKey; // ' '
- gMessageModifiers = 0;
- }
- else if(strcmp(s, kStopCmd) == 0)
- {
- gMessageToPost = kPeriodKey; // cmd-.
- gMessageModifiers = cmdKey;
- }
- else if(strcmp(s, kFastFwdCmd) == 0)
- {
- gMessageToPost = kRightArrowKey; // cmd ->
- gMessageModifiers = cmdKey;
- }
- else if(strcmp(s, kRewindCmd) == 0)
- {
- gMessageToPost = kLeftArrowKey; // cmd <-
- gMessageModifiers = cmdKey;
- }
- else if(strcmp(s, kNextCmd) == 0)
- {
- gMessageToPost = kRightArrowKey; // -> arrow key
- gMessageModifiers = 0;
- }
- else if(strcmp(s, kPrevCmd) == 0)
- {
- gMessageToPost = kLeftArrowKey; // <- arrow key
- gMessageModifiers = 0;
- }
- else if(strcmp(s, kEjectCmd) == 0)
- {
- gMessageToPost = kEKey; // cmd-E
- gMessageModifiers = cmdKey;
- }
- else if(strcmp(s, kVolUpCmd) == 0)
- {
- gMessageToPost = kUpArrowKey; // cmd-up
- gMessageModifiers = cmdKey;
- }
- else if(strcmp(s, kVolDownCmd) == 0)
- {
- gMessageToPost = kDownArrowKey; // cmd-down
- gMessageModifiers = cmdKey;
- }
- else if(strcmp(s, kMuteCmd) == 0)
- {
- gMessageToPost = kMKey; // cmd-M
- gMessageModifiers = cmdKey;
- }
- else if(strcmp(s, kDVDMenuCmd) == 0)
- {
- gMessageToPost = kEscapeKey; // escape
- gMessageModifiers = 0;
- }
- else if(strcmp(s, kSizeTinyCmd) == 0)
- {
- gMessageToPost = k0Key; // cmd-0
- gMessageModifiers = cmdKey;
- }
- else if(strcmp(s, kSizeHalfCmd) == 0)
- {
- gMessageToPost = k1Key; // cmd-1
- gMessageModifiers = cmdKey;
- }
- else if(strcmp(s, kSizeNormCmd) == 0)
- {
- gMessageToPost = k2Key; // cmd-2
- gMessageModifiers = cmdKey;
- }
- else if(strcmp(s, kSizeFullCmd) == 0)
- {
- gMessageToPost = k3Key; // cmd-3
- gMessageModifiers = cmdKey;
- }
- else if(strcmp(s, kPresentCmd) == 0)
- {
- gMessageToPost = kPKey; // cmd-option-P
- gMessageModifiers = cmdKey | optionKey;
- }
-
- return;
- }
- */
- // -------------------
-
- short SerialRead(short numBytes, char *bufP)
- {
- OSErr checkBufErr, err;
- long charCount = 0;
- // long lastCount = TickCount();
- // EventRecord myEvent;
-
- while(charCount < numBytes)
- {
- //WaitNextEvent(0, &myEvent, 5, nil);
- EventLoop(false);
-
- checkBufErr = SerGetBuf(gInRefNum, &charCount);
-
- // if((TickCount() - lastCount) > (60*60))
- // {
- // Debugger();
- // lastCount = TickCount();
- // }
-
- }
-
- charCount = numBytes;
-
- err = FSRead(gInRefNum, &charCount, bufP);
-
- if(numBytes == sizeof(MyPacket) &&
- ((*(long*)bufP) & 0x00FFFFFF) == '\0hac')
- {
- memmove(bufP, bufP+1, sizeof(MyPacket)-1);
- charCount = 1;
- err = FSRead(gInRefNum, &charCount, bufP+ sizeof(MyPacket)-1);
- }
-
- return err;
-
- /* checkBufErr = SerGetBuf(gInRefNum, &charCount);
- if (checkBufErr == noErr) {
-
- // The general strategy here is this: if number of available characters
- // meets a certain minimum threshold, then I read in everything in the
- // buffer. If I get delayed, I'll catch up quickly.
-
- // if (charCount != 0 && charCount >= reqBytes) {
- if (charCount > 0) {
-
- serRdErr = FSRead(gInRefNum, &charCount, &gBitBucket[gBitBucketCount]);
- if (serRdErr == noErr) {
- gBitBucketCount += charCount;
- // gBitBucket[gBitBucketCount] = 0xFF; // append an 'unfinished string' terminator
- }
- }
- }
- }*/
- }
-
- short SerialWrite(short numBytes, char *bufP)
- {
- long charCount = numBytes;
-
- return FSWrite(gOutRefNum, &charCount, bufP);
-
- }
-
-
- Err PrvTranslateMacErr(OSErr macErr)
- {
- Err err;
-
- switch (macErr)
- {
- case noErr: err = errNone; break;
- case fnfErr: err = vfsErrFileNotFound; break;
- case eofErr: err = vfsErrFileEOF; break;
- case dupFNErr: err = vfsErrFileAlreadyExists; break;
-
- default:
- err = -1;
- break;
- }
-
- return err;
- }
-
- Err PrvTranslatePalmPath(const Char *palmPathP)
- {
- unsigned char* dstP;
-
- if(strlen(palmPathP) >= (kPathBufferLen - 1)) return -1;
-
- // clear string:
- gPathBuffer[0]=0;
-
- // start writing after length byte:
- dstP = &gPathBuffer[1];
-
- // if there's a leading slash, remove it... we want to go from the root.
- // if(palmPathP[0] == '/')
- // palmPathP++;
-
- // copy characters, translating '/' to ':', and incrementing length.
- while(*palmPathP != '\0')
- {
- if(*palmPathP == '/')
- {
- *dstP = ':';
- }
- else
- {
- *dstP = *palmPathP;
- }
- dstP++;
- palmPathP++;
- gPathBuffer[0]++;
- }
-
-
- return errNone;
- }
-
- short LocalDirIterate(long ref, unsigned long *dirEntryP, char *buf, long *size, long *attr)
- {
- short index;
- Err err;
- OSErr macErr;
- unsigned char nameBuf[32];
- // long dirID;
- short len;
-
- if(*dirEntryP == expIteratorStop)
- return expErrEnumerationEmpty;
-
- // err = PrvFindDirID(dirPathNameP, &dirID);
- // if(err) return err;
-
- // make sure its a directory:
- if(gDirOpen == false) return vfsErrFileBadRef;
- if(ref != kDirectoryFileRefToken) return vfsErrNotADirectory;
-
-
- memset(&gMyCPB, 0x00, sizeof(gMyCPB)); // clear param block
-
- gPathBuffer[0] = 0;
- gMyCPB.hFileInfo.ioCompletion = NULL;
-
- gMyCPB.hFileInfo.ioNamePtr = nameBuf;
- gMyCPB.hFileInfo.ioFRefNum = 0;
-
- // Get index from iterator value:
- index = (short)*dirEntryP;
- if(index == 0) index = 1; // if its the 1st time through, bump the index to 1 since its actually 1-based.
-
- gMyCPB.hFileInfo.ioFDirIndex = index;
- gMyCPB.hFileInfo.ioVRefNum = gCurVRefNum;
- gMyCPB.hFileInfo.ioDirID = gIterateDirID;//gBaseDirID;
-
- macErr = PBGetCatInfo(&gMyCPB, false);
- err = PrvTranslateMacErr(macErr);
-
- if(err == errNone)
- {
- // convert name to c string:
- len = nameBuf[0];
- memmove(&nameBuf[0], &nameBuf[1], len);
- nameBuf[len] = '\0';
-
- // copy name:
- strcpy(buf, (char*)nameBuf);
- *size = len;
-
- // Fill out attributes:
- *attr = 0;
- if(gMyCPB.hFileInfo.ioFlAttrib & 0x0001) *attr |= vfsFileAttrReadOnly;
- if(gMyCPB.hFileInfo.ioFlAttrib & 0x0010) *attr |= vfsFileAttrDirectory;
- }
-
- index++; // point index to next file
-
- // make another call to see if we're at the end...
-
- memset(&gMyCPB, 0x00, sizeof(gMyCPB)); // clear param block...
-
- gMyCPB.hFileInfo.ioCompletion = NULL;
- gMyCPB.hFileInfo.ioNamePtr = nameBuf;
- gMyCPB.hFileInfo.ioFRefNum = 0;
-
- gMyCPB.hFileInfo.ioFDirIndex = index;
- gMyCPB.hFileInfo.ioVRefNum = gCurVRefNum;
- gMyCPB.hFileInfo.ioDirID = gIterateDirID;
-
- macErr = PBGetCatInfo(&gMyCPB, false);
-
-
- if(macErr) *dirEntryP = expIteratorStop;
- else *dirEntryP = index;
-
- return err;
- }
-
- short LocalFileCreate(char *path)
- {
- OSErr macErr;
- Err err;
-
- PrvTranslatePalmPath(path);
-
- macErr = Create(gPathBuffer, 0, 'PALM', 'File');
- err = PrvTranslateMacErr(macErr);
-
- return err;
- }
-
- short LocalDirCreate(char *path)
- {
- OSErr macErr;
- Err err;
- long dirID;
-
- PrvTranslatePalmPath(path);
-
- // FSMakeFSSpec(0, 0, gPathBuffer, &spec);
- macErr = DirCreate(0,0,gPathBuffer,&dirID);
- err = PrvTranslateMacErr(macErr);
-
- return err;
- }
-
- // given a full pathname to a directory, find the dirID
- Err PrvFindDirID(const Char *dirPathNameP, long *dirID)
- {
- Err err;
-
- PrvTranslatePalmPath(dirPathNameP);
-
- gMyCPB.hFileInfo.ioCompletion = NULL;
- gMyCPB.hFileInfo.ioNamePtr = gPathBuffer;
- gMyCPB.hFileInfo.ioFRefNum = 0;
- gMyCPB.hFileInfo.ioFDirIndex = 0;
- gMyCPB.hFileInfo.ioVRefNum = gCurVRefNum;
- gMyCPB.hFileInfo.ioDirID = gBaseDirID;
-
- err = PrvTranslateMacErr(PBGetCatInfo(&gMyCPB, false));
-
- if(err == errNone)
- {
- if(gMyCPB.hFileInfo.ioFlAttrib & 0x0010) *dirID = gMyCPB.hFileInfo.ioDirID;
- else err = vfsErrNotADirectory;
- }
-
- return err;
- }
-
- short LocalFileOpen(char *path, long /*perm*/, long *fileRefP)
- {
- Err err = PrvTranslatePalmPath(path);
- OSErr macErr;
- short macRef;
- long dirID;
-
- if(err) return err;
-
- err = PrvFindDirID(path, &dirID);
- if(err == errNone)
- {
- // if iterating root, save path:
- // if(StrCompare(pathNameP, "/Palm/Launcher") == 0)
- // {
- if(gDirOpen)
- {
- return vfsErrFileStillOpen;
- }
-
- macErr = HGetVol(NULL, &gCurVRefNum, &gBaseDirID);
- err = PrvTranslateMacErr(macErr);
- if(err)
- {
- gBaseDirID = 0;
- gCurVRefNum = 0;
- return err;
- }
-
- gIterateDirID = dirID;
-
- *fileRefP = kDirectoryFileRefToken;
- gDirOpen = true;
- return errNone;
- // }
- }
-
- macErr = FSOpen(gPathBuffer, 0, &macRef);
- err = PrvTranslateMacErr(macErr);
-
- // DOLATER-jesse: fix this to work with directories other than the root...
-
- if(err == errNone) *fileRefP = macRef;
- else *fileRefP = 0;
-
- return err;
- }
-
- short LocalFileClose(long filep)
- {
- if(filep == kDirectoryFileRefToken && gDirOpen == true)
- {
- gDirOpen = false;
- gIterateDirID = 0;
- return errNone;
- }
-
- return PrvTranslateMacErr(FSClose((short)filep));
- }
-
- short LocalFileRead(long fileRef, long numBytes, long *numBytesRead, char *bufP)
- {
- Int32 count = (Int32)numBytes;
- Err err;
-
- err = PrvTranslateMacErr(FSRead(fileRef, &count, bufP));
- if(numBytesRead) *numBytesRead = count;
-
- return err;
- }
-
-
- short LocalFileWrite(long fileRef, long numBytes, long *numWritten, char *bufP)
- {
- Int32 count = (Int32)numBytes;
- Err err;
-
- err = PrvTranslateMacErr(FSWrite(fileRef, &count, bufP));
- if(numWritten) *numWritten = count;
-
- return err;
- }
-
-
- short LocalFileSeek(long fileRef, short origin, unsigned short offset)
- {
- short mode;
-
- switch(origin)
- {
- case vfsOriginBeginning:
- mode = 1;
- break;
- case vfsOriginEnd:
- mode = 2;
- break;
- case vfsOriginCurrent:
- mode = 3;
- break;
-
- default:
- DebugStr("\pinvalid FileOrigin");
- }
-
- return PrvTranslateMacErr(SetFPos((short)fileRef, mode, offset));
- }
-
- short LocalFileSize(long fileRef, long *size)
- {
- OSErr osErr = GetEOF(fileRef, size);
-
- return PrvTranslateMacErr(osErr);
- }
-
- short LocalFileResize(long fileRef, long newsize)
- {
- OSErr osErr = SetEOF(fileRef, newsize);
-
- return PrvTranslateMacErr(osErr);
- }
-
- short LocalFileDelete(char *path)
- {
- OSErr macErr;
- Err err;
-
- PrvTranslatePalmPath(path);
-
- macErr = HGetVol(NULL, &gCurVRefNum, &gBaseDirID);
- err = PrvTranslateMacErr(macErr);
- if(err)
- {
- gBaseDirID = 0;
- gCurVRefNum = 0;
- return err;
- }
-
- macErr = HDelete(gCurVRefNum, gBaseDirID, gPathBuffer);
- err = PrvTranslateMacErr(macErr);
-
- return err;
- }
-